import { Logger, UseGuards, UseInterceptors } from '@nestjs/common';
import { MessageBody, OnGatewayConnection, OnGatewayDisconnect, OnGatewayInit, SubscribeMessage, WebSocketGateway, WebSocketServer, } from '@nestjs/websockets';
import { WsServiceResponseInterceptor } from './ws-service.response.interceptor';
import { UsersService } from 'src/service/HTTPService/users/users.service';
import { jwtConstants } from 'src/constants/constants';
import { JwtService } from '@nestjs/jwt';
import { Server, Socket } from 'socket.io';
import { Room } from 'src/service/Room/ws.room';
import { AuthGuard } from 'src/guard/auth.guard';

// 定义WebSocket网关，绑定端口3002
@WebSocketGateway(3002)
// 使用拦截器，用于处理WebSocket服务的响应
@UseInterceptors(new WsServiceResponseInterceptor())
// 再次定义WebSocketGateway，指定传输协议为websocket
@WebSocketGateway({ transports: ['websocket'] })
export class MyWebSocketGateway {
    constructor(private readonly usersService: UsersService, private jwtService: JwtService) {
        // 构造函数，注入UsersService和JwtService

    }
    // 日志记录器，用于记录日志信息
    private logger: Logger = new Logger('Gateway');
    // WebSocket服务器实例
    private server: Server = new Server();
    private roomList: Room[] = [];

    /**
     * 处理客户端发送的消息
     * @param client 发送消息的客户端Socket实例
     * @param data 客户端发送的消息数据
     * @returns 返回接收到的数据
     */
    @SubscribeMessage('message')
    handleMessage(client: Socket, @MessageBody() data: any) {
        console.log('[message]接收消息：', data);
        return data;
    }
    @SubscribeMessage('getRoomList')
    async getRoomId(@MessageBody() data: any) {
        console.log('[getRoomId]接收消息：', data);
        return this.roomList.length;
    }

    @SubscribeMessage('createRoom')
    async createRoom(client: Socket, @MessageBody() data: any) {
        console.log('[createRoom]接收消息：', data);
        console.log('[createRoom]接收消息：', client);
    }


}